package com.feiyuan.core.controller.admin;

import com.feiyuan.common.result.R;
import com.feiyuan.core.annotation.FeiyuanRequiresPermissions;
import com.feiyuan.core.pojo.entity.SysQuartzJob;
import com.feiyuan.core.service.SysQuartzJobService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 任务调度
 */
@Api("任务调度")
@RestController
@RequestMapping("/admin/core/quartz")
public class AdminQuartzJobController {

    @Autowired
    private SysQuartzJobService sysQuartzJobService;

    @GetMapping("/list")
    @ApiOperation("查询任务列表")
    @FeiyuanRequiresPermissions("admin:quartz:list")
    public R list(@RequestParam Integer page, @RequestParam Integer limit) {
        IPage<SysQuartzJob> sysQuartzJobIPage = sysQuartzJobService.listPage(page, limit);
        return R.ok().data("pages", sysQuartzJobIPage);
    }

    @GetMapping("/info/{id}")
    @ApiOperation("查询任务详情")
    @FeiyuanRequiresPermissions("admin:quartz:info")
    public R info(@PathVariable Long id) {
        return R.ok().data("info", sysQuartzJobService.getById(id));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存或者修改")
    @FeiyuanRequiresPermissions("admin:quartz:saveOrUpdate")
    public R saveOrUpdate(@RequestBody SysQuartzJob quartzJob) {
        return R.ok().data("data", sysQuartzJobService.saveOrUpdateJob(quartzJob));
    }

    @PostMapping("/remove/{id}")
    @ApiOperation("删除任务")
    @FeiyuanRequiresPermissions("admin:quartz:remove")
    public R remove(@PathVariable Long id) {
        return R.ok().data("data", sysQuartzJobService.removeJob(id));
    }

    @PostMapping("/stopJob/{id}")
    @ApiOperation("暂停定时任务")
    @FeiyuanRequiresPermissions("admin:quartz:stopJob")
    public R stopJob(@PathVariable Long id) {
        return R.ok().data("data", sysQuartzJobService.stopJob(id));
    }

    @PostMapping("/resumeJob/{id}")
    @ApiOperation("恢复定时任务")
    @FeiyuanRequiresPermissions("admin:quartz:resumeJob")
    public R resumeJob(@PathVariable Long id) {
        return R.ok().data("data", sysQuartzJobService.resumeJob(id));
    }

    @PostMapping("/runJob/{id}")
    @ApiOperation("立即执行定时任务")
    @FeiyuanRequiresPermissions("admin:quartz:runJob")
    public R runJob(@PathVariable Long id) {
        return R.ok().data("data", sysQuartzJobService.runJob(id));
    }
}
